import { myFollowedDynamic } from '../../../api/user'
import { formatTime } from "../../../utils/util";

Component({
  properties: {

  },

  data: {
    list: [],
    listQuery: {
      condition: {
        keyword: ''
      },
      pager: {
        page: 1,
        pageSize: 10
      }
    },
    isEnd: false,
    isNoData: false
  },

  options: {
    styleIsolation: 'apply-shared'
  },

  lifetimes: {
    attached() {
      this.init()
    }
  },

  attached() {
    this.init()
  },

  methods: {
    init() {
      this.getMore()
    },
    getMore() {
      const _this = this
      myFollowedDynamic(this.data.listQuery).then(res => {
        let list = JSON.parse(JSON.stringify(_this.data.list))
        if (this.data.listQuery.pager.page === 1 && !res.records.length) {
          this.setData({ isNoData: true })
          return
        }
        if (res.records.length) {
          res.records.forEach(re => {
            re.createTime = formatTime(re.createTime, '{y}.{m}.{d} {h}:{i}')
            re.content.startTime = formatTime(re.content.startTime, '{y}.{m}.{d} {h}:{i}')
            re.content.endTime = formatTime(re.content.endTime, '{y}.{m}.{d} {h}:{i}')
          })
          list = list.concat(res.records)
        }
        this.setData({
          list: list
        })
        if (res.records.length < 10) {
          this.setData({ isEnd: true })
        }
      })
    },
    toLower() {
      if (!this.data.isEnd) {
        const page = this.data.listQuery.pager.page + 1
        this.setData({
          'listQuery.pager.page': page
        })
        this.getMore()
      }
    },
    onSearch(e) {
      this.setData({
        listQuery: {
          condition: {
            keyword: e.detail
          },
          pager: {
            page: 1,
            pageSize: 10
          }
        },
        list: [],
        isEnd: false,
        isNoData: false
      })
      this.getMore()
    }
  }
})
